<?php

/**
 * 商品数据
 * Class stat_transdata
 */
class Stat_goodsdataApp extends BackendApp
{
    /**
     * 默认首页
     */
    function index()
    {
        // 显示错误
        ini_set('display_errors', 'On');
        error_reporting(E_ALL);

        // 后台登录信息
        $this->assign('admin', $this->visitor->get());

        // 准备工作
        // SKU表
        $gcategory_mod = &m('gcategory');
        $goods_mod = &m('goods');
        $goodsspec_mod = &m('goodsspec');
        $ordergoods_mod = &m('ordergoods');

        // SKU数量
        $goods_sku_number = $goodsspec_mod->getOne("select count(*) from {$goodsspec_mod->table}");
        $this->assign('goods_sku_number', $goods_sku_number);

        // SKU销量，需要统计ecm_order_goods表中quantity的汇总
        $goods_sku_sellnumber = $ordergoods_mod->getOne("select sum(`quantity`) from {$ordergoods_mod->table}");
        $this->assign('goods_sku_sellnumber', $goods_sku_sellnumber);

        // SPU数量
        $goods_spu_number = $goods_mod->getOne("select count(*) from {$goods_mod->table}");
        $this->assign('goods_spu_number', $goods_spu_number);

        // 一级类目数量
        $goods_cate_number = $gcategory_mod->getOne("select count(*) from {$gcategory_mod->table} WHERE parent_id=0");
        $this->assign('goods_cate_number', $goods_cate_number);

        // 二级类目数量
        $goods_secondcate_number = $gcategory_mod->getOne("select count(*) from {$gcategory_mod->table} WHERE parent_id>0");
        $this->assign('goods_secondcate_number', $goods_secondcate_number);

        // 商品总库存
        $goods_sum_number = $goodsspec_mod->getOne("SELECT SUM(stock) FROM `{$goodsspec_mod->table}` WHERE stock>0 and spec_3value>0");
        $this->assign('goods_sum_number', $goods_sum_number);

        // 商品总价格
        $goods_sum_price = $goodsspec_mod->getOne("SELECT SUM(stock*price) FROM `{$goodsspec_mod->table}` WHERE stock>0 and spec_3value>0");
        $this->assign('goods_sum_price', $goods_sum_price);

        // 商品所属品牌的数据情况，见下方的getbrandsandgoods方法

        // 调用模板
        $this->display('stat_goodsdata.index.html');
    }

    /**
     * 获取商品所属品牌的数据情况
     */
    function getbrandsandgoods()
    {
        // 逻辑
        $goods_mod = &m('goods');
        $datas = $goods_mod->getAll("select count(goods_id) as value, brand as name from {$goods_mod->table} group by brand");
        echo json_encode($datas);
    }
}
